package com.feihong.txgw.adapter.product;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.adapter.base.BaseQuickAdapter;
import com.adapter.base.BaseViewHolder;
import com.adapter.base.animation.ScaleInAnimation;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.feihong.txgw.R;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ProductListAdapter
 * @package com.feihong.txgw.adapter.product
 * @data 2018/6/5 10:58
 * @Describe
 * @Change
 */
public class ProductListAdapter extends BaseQuickAdapter<TerminalEntiyt,BaseViewHolder> {

    public ProductListAdapter(int layoutResId, @Nullable List<TerminalEntiyt> data) {
        super(layoutResId, data);
        init();
    }

    public ProductListAdapter(int layoutResId) {
        super(layoutResId);
        init();
    }

    private void init() {
        setEnableLoadMore(false);
        openLoadAnimation(new ScaleInAnimation(0.8f));
        isFirstOnly(false);
    }

    @Override
    protected void convert(BaseViewHolder helper, TerminalEntiyt item) {
        helper.addOnClickListener(R.id.add_shopcar_icon);
        StringBuffer stringBuffer = new StringBuffer();
        try {
            if(!TextUtils.isEmpty(item.getSpec())){
                JSONArray jsonArray = new JSONArray(item.getSpec());
                if(jsonArray != null){
                    for(int i = 0 ; i < jsonArray.length() ; i ++){
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"   ");
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        helper.setText(R.id.txt_title,item.getSku_name());
        helper.setText(R.id.txt_des,stringBuffer.toString());
        helper.setText(R.id.txt_price,"¥" + item.getShop_price());

        RequestOptions requestOptions = new RequestOptions()
                .error(R.mipmap.ic_launcher);
        Glide.with(mContext).load(item.getThumb()).apply(requestOptions).into((ImageView) helper.getView(R.id.imageView));
    }
}
